package com.papper.mng.controller.google;

import com.papper.common.bean.sys.SysSystem;
import com.papper.common.bean.sys.SysUser;
import com.papper.common.service.sys.SysLimitService;
import com.papper.common.service.sys.SysModelService;
import com.papper.common.service.sys.SysResourceService;
import com.papper.common.service.sys.SysSystemService;
import com.papper.common.service.sys.SysUserService;
import com.papper.mng.controller.BaseAction;
import com.papper.mng.domain.DwzResponse;
import com.papper.mng.utils.tool.GoogleAuthenticator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import tk.mybatis.mapper.util.StringUtil;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author:Lajiao
 * @Date:2014年8月21日
 * @Time:下午12:42:13
 * @Description:谷歌验证码绑定
 */
@Controller
public class GoogleAuthController extends BaseAction {
    @Autowired
    SysUserService     sysUserService;
    @Autowired
    SysResourceService sysResourceService;
    @Autowired
    SysSystemService   sysSystemService;
    @Autowired
    SysLimitService    sysLimitService;
    @Autowired
    SysModelService    sysModelService;

    @RequestMapping(value = "/google/auth", method = RequestMethod.GET)
    public String googleAuth(Model model) {
        SysUser sysUser = getLoginUser();
        if (sysUser == null) {
            return render("google/auth");
        }
        if (isSuperUser()) {
            return render("google/auth");
        }
        SysUser dbSysUser = sysUserService.findById(sysUser.getId());
        if (dbSysUser == null) {
            return render("google/auth");
        }
        String isFirstBind = "1";
        if (StringUtil.isEmpty(dbSysUser.getKeyCode())) {
            // 首次绑定
            isFirstBind = "1";
        } else {
            // 重新绑定
            isFirstBind = "0";
        }
        model.addAttribute("isFirstBind", isFirstBind);
        SysSystem sysSystem = sysSystemService.findById(dbSysUser.getSystemId());
        String    secret    = GoogleAuthenticator.generateSecretKey();
        model.addAttribute("generateSecretKey", secret);
        String qrcode = GoogleAuthenticator.getQRBarcodeURL(dbSysUser.getId().toString(), sysSystem.getSystemHost(), secret);
        model.addAttribute("getQRBarcodeURL", qrcode);
        return render("google/auth");
    }

    @RequestMapping(value = "/google/auth", method = RequestMethod.POST)
    @ResponseBody
    public DwzResponse googleAuthPost(HttpServletResponse _response, String oldPass, String newPass, String generateSecretKey) throws IOException {
        SysUser sysUser = getLoginUser();
        if (sysUser == null) {
            return new DwzResponse("300", "尚未登录！");
        }
        if (isSuperUser() || sysUser.getId().intValue() == 0) {
            return new DwzResponse("300", "超级管理员不能修改授权！");
        }
        SysUser dbSysUser = sysUserService.findById(sysUser.getId());
        if (dbSysUser == null) {
            return new DwzResponse("300", "尚未登录！");
        }
        if (StringUtil.isEmpty(newPass) || StringUtil.isEmpty(generateSecretKey)) {
            return new DwzResponse("300", "新绑定授权码信息为空！");
        }
        if (!StringUtil.isEmpty(dbSysUser.getKeyCode())) {
            if (StringUtil.isEmpty(oldPass)) {
                return new DwzResponse("300", "已绑定授权码信息为空！");
            }
            boolean r = GoogleAuthenticator.authcode(oldPass, dbSysUser.getKeyCode());
            if (!r) {
                return new DwzResponse("300", "已绑定授权码校验不通过！");
            }
        }
        boolean r = GoogleAuthenticator.authcode(newPass, generateSecretKey);
        if (!r) {
            return new DwzResponse("300", "新绑定授权码校验不通过！");
        }
        // 授权修改
        SysUser modify = new SysUser();
        modify.setId(dbSysUser.getId());
        modify.setKeyCode(generateSecretKey);
        sysUserService.update(modify);
        return new DwzResponse("200", "授权修改成功！", "closeCurrent");
    }
}
